package cn.controlsys.common.saiheyi;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;

public class ParameterBuilder {
    HashMap<String, String> map = new HashMap<String, String>();
    HashMap<String, String> encodeMap = new HashMap<String, String>();
    public ParameterBuilder() {
    }
    public void put(String key, String value) {
        map.put(key, value);
    }
    public void put(String key, String value, String charsetName) {
        try {
            map.put(key, value);
            if (charsetName == null || "".equals(charsetName.trim())) {
                charsetName = "utf-8";
            }
            encodeMap.put(key, URLEncoder.encode(value, charsetName));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public String toString() {
        return this.toString(true);
    }
    private String toString(boolean encode){
        StringBuilder sb = new StringBuilder();
        try {
            String[] keyArr = map.keySet().toArray(new String[0]);
            Arrays.sort(keyArr);
            for (String key : keyArr) {
                String value = map.get(key);
                if (encode && encodeMap.containsKey(key)) {
                    value = encodeMap.get(key);
                }
                sb.append(key.toLowerCase()).append("=").append(value).append("&");
            }
            sb.deleteCharAt(sb.lastIndexOf("&"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
    /**
     * MD5签名
     * @param signKey
     * @return
     */
    public String sign(String signKey) {
        String result = "";
        String data = this.toString(false);
        if (signKey != null) {
            data = data + signKey.trim();
        }
        try {
// 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
// 计算md5函数
            md.update(data.getBytes("utf-8"));
// digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
// BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
            BigInteger bi = new BigInteger(1, md.digest());
// data = bi.toString(16);
// BigInteger会把0省略掉，需补全至32位
            result = String.format("%032x", bi); // 左侧用0补够32位长度 prints:
// 00000000000000000093
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * Description: 生成订单号
     * @return
     * @param wecaccount 水电煤缴费账号
     * @author yzc
     * @date 13:45 2018/4/3
     */
    public static String getOutTradeNo(String wecaccount){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        date = date.substring(date.length()-8,date.length());
        String[] split = date.split("-");
        String string = "";
        for(int i = 0;i<split.length;i++){
            string=string+split[i];
        }
        String time = System.currentTimeMillis()/1000+"";
        time = time.substring(time.length()-5,time.length());
        long start = System.currentTimeMillis();
        long end = System.currentTimeMillis();
        String str = ((int)(Math.random()*900)+100)+"";

        return string+time+wecaccount+str;

    }

    /**
     * Description: 获取签名后的字符串
     * @return
     * @param wecaccount 水电煤缴费账号
     * @author yzc
     * @date 13:45 2018/4/3
     */
    public static String getSignStr(String wecaccount){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        date = date.substring(date.length()-8,date.length());
        String[] split = date.split("-");
        String string = "";
        for(int i = 0;i<split.length;i++){
            string=string+split[i];
        }
        String time = System.currentTimeMillis()/1000+"";
        time = time.substring(time.length()-5,time.length());
        long start = System.currentTimeMillis();
        long end = System.currentTimeMillis();
        String str = ((int)(Math.random()*900)+100)+"";

        return string+time+wecaccount+str;

    }

}
